'''
Created on 27.12.2011

@author: hkh
'''
from pyxll import xl_func
from CashFlowUtils.IceBonds import *
from IRUtils.REIBOR import *
from TimeUtils.daterolling import DateRollingConvention
from TimeUtils.daycount import *
from TimeUtils.utils import *
from datetime import date
from numpy import *
from scipy.interpolate import interp1d
from scipy import interpolate


@xl_func("date valuedate, date fwddate, float[] rates: float")
def curveshit(valuedate, fwddate, rates):

    curvepoints = []
    yearfracfwd = yearfraction(valuedate, fwddate, DayCountBasis().basis_actact)

    curvepoints.append(REIBOR1M(valuedate, rates[0][0]))
    curvepoints.append(REIBOR2M(valuedate, rates[1][0]))
    curvepoints.append(REIBOR3M(valuedate, rates[2][0]))
    curvepoints.append(REIBOR6M(valuedate, rates[3][0]))
    curvepoints.append(REIBOR9M(valuedate, rates[4][0]))
    curvepoints.append(REIBOR12M(valuedate, rates[5][0]))

    curvepoints.sort(key=lambda point: point.maturitydate)

    for obj in curvepoints:
        print obj.maturitydate, obj.discountfactor(), obj.continuousrate(), obj.yearfrac()

    t = array([obj.yearfrac() for obj in curvepoints])
    y = array([obj.continuousrate() for obj in curvepoints])

    f1 = interp1d(t, y)

    if yearfracfwd < t[0]:
        y = y[0]
    elif yearfracfwd > t[-1]:
        y = y[-1]
    else:
        y = f1(yearfracfwd)
     
    return math.exp(-y*yearfracfwd)
    

@xl_func("date ddate: float")
def ibdailycpi(ddate):
    cpicurve = CpiCurve()
    return cpicurve.dailycpi(ddate)

@xl_func("date ddate: float")
def ibdailycpihff(ddate):
    cpicurve = CpiCurve()
    return cpicurve.dailycpihff(ddate)

@xl_func("string sec, date ddate, float yld: float")
def ibdirty(sec, ddate, yld):
    if sec == "HFF150914":
        bond = HFF150914()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "HFF150224":
        bond = HFF150224()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "HFF150434":
        bond = HFF150434()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "HFF150644":
        bond = HFF150644()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RVK091":
        bond = RVK091()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "LSS150224":
        bond = LSS150224()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 12 0824":
        bond = RIKB120824()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 13 0517":
        bond = RIKB130517()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 16 1013":
        bond = RIKB161013()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 19 0226":
        bond = RIKB190226()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 22 1026":
        bond = RIKB221026()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 25 0612":
        bond = RIKB250612()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKB 31 0124":
        bond = RIKB310124()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKS 21 0414":
        bond = RIKS210414()
        res = bond.dirtyprice(ddate, yld)
    elif sec == "RIKS 30 0701":
        bond = RIKS300701()
        res = bond.dirtyprice(ddate, yld)
    else:
        res = 0
    return res

@xl_func("string sec, date ddate, float yld: float")
def ibclean(sec, ddate, yld):
    if sec == "HFF150914":
        bond = HFF150914()
        res = bond.cleanprice(ddate, yld)
    elif sec == "HFF150224":
        bond = HFF150224()
        res = bond.cleanprice(ddate, yld)
    elif sec == "HFF150434":
        bond = HFF150434()
        res = bond.cleanprice(ddate, yld)
    elif sec == "HFF150644":
        bond = HFF150644()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RVK091":
        bond = RVK091()
        res = bond.cleanprice(ddate, yld)
    elif sec == "LSS150224":
        bond = LSS150224()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 12 0824":
        bond = RIKB120824()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 13 0517":
        bond = RIKB130517()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 16 1013":
        bond = RIKB161013()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 19 0226":
        bond = RIKB190226()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 22 1026":
        bond = RIKB221026()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 25 0612":
        bond = RIKB250612()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 31 0124":
        bond = RIKB310124()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKS 21 0414":
        bond = RIKS210414()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKS 30 0701":
        bond = RIKS300701()
        res = bond.cleanprice(ddate, yld)
    else:
        res = 0
    return res

@xl_func("string sec, date ddate, float price: float")
def ibcleantodirty(sec, ddate, price):
    if sec == "HFF150914":
        bond = HFF150914()
        res = bond.cleantodirty(ddate, price)
    elif sec == "HFF150224":
        bond = HFF150224()
        res = bond.cleantodirty(ddate, price)
    elif sec == "HFF150434":
        bond = HFF150434()
        res = bond.cleantodirty(ddate, price)
    elif sec == "HFF150644":
        bond = HFF150644()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RVK091":
        bond = RVK091()
        res = bond.cleantodirty(ddate, price)
    elif sec == "LSS150224":
        bond = LSS150224()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 12 0824":
        bond = RIKB120824()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 13 0517":
        bond = RIKB130517()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 16 1013":
        bond = RIKB161013()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 19 0226":
        bond = RIKB190226()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 22 1026":
        bond = RIKB221026()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 25 0612":
        bond = RIKB250612()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKB 31 0124":
        bond = RIKB310124()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKS 21 0414":
        bond = RIKS210414()
        res = bond.cleantodirty(ddate, price)
    elif sec == "RIKS 30 0701":
        bond = RIKS300701()
        res = bond.cleantodirty(ddate, price)
    else:
        res = 0
    return res

@xl_func("string sec, date ddate, float yld: float")
def ibduration(sec, ddate, yld):
    if sec == "HFF150914":
        bond = HFF150914()
        res = bond.duration(ddate, yld)
    elif sec == "HFF150224":
        bond = HFF150224()
        res = bond.duration(ddate, yld)
    elif sec == "HFF150434":
        bond = HFF150434()
        res = bond.duration(ddate, yld)
    elif sec == "HFF150644":
        bond = HFF150644()
        res = bond.duration(ddate, yld)
    elif sec == "RVK091":
        bond = RVK091()
        res = bond.duration(ddate, yld)
    elif sec == "LSS150224":
        bond = LSS150224()
        res = bond.cleanprice(ddate, yld)
    elif sec == "RIKB 12 0824":
        bond = RIKB120824()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 13 0517":
        bond = RIKB130517()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 16 1013":
        bond = RIKB161013()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 19 0226":
        bond = RIKB190226()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 22 1026":
        bond = RIKB221026()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 25 0612":
        bond = RIKB250612()
        res = bond.duration(ddate, yld)
    elif sec == "RIKB 31 0124":
        bond = RIKB310124()
        res = bond.duration(ddate, yld)
    elif sec == "RIKS 21 0414":
        bond = RIKS210414()
        res = bond.duration(ddate, yld)
    elif sec == "RIKS 30 0701":
        bond = RIKS300701()
        res = bond.duration(ddate, yld)
    else:
        res = 0
    return res

@xl_func("string sec, date ddate, float yld: float")
def ibyield(sec, ddate, price):
    if sec == "RIKB 12 0824":
        bond = RIKB120824()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 13 0517":
        bond = RIKB130517()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 16 1013":
        bond = RIKB161013()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 19 0226":
        bond = RIKB190226()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 22 1026":
        bond = RIKB221026()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 25 0612":
        bond = RIKB250612()
        res = bond.yld(ddate, price)
    elif sec == "RIKB 31 0124":
        bond = RIKB310124()
        res = bond.yld(ddate, price)
    elif sec == "RIKS 21 0414":
        bond = RIKS210414()
        res = bond.yld(ddate, price)
    elif sec == "RIKS 30 0701":
        bond = RIKS300701()
        res = bond.yld(ddate, price)
    else:
        res = 0
    return res